import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from 'components/Login.vue'
// import Home from 'components/Home.vue'
// import Welcome from 'components/Welcome.vue'
// import User from 'components/user/User.vue'
// import Order from 'components/order/Order.vue'
// import Rights from 'components/power/Rights.vue'
// import Roles from 'components/power/Roles.vue'
// import Cate from 'components/goods/Cate.vue'
// import Params from 'components/goods/Params.vue'
// import List from 'components/goods/List.vue'
// import Add from 'components/goods/Add.vue'
// import Report from 'components/report/Report.vue'

const Login = () => import('components/Login.vue')
const Home = () => import('components/Home.vue')
const Welcome = () => import('components/Welcome.vue')
const User = () => import('components/user/User.vue')
const Order = () => import('components/order/Order.vue')
const Rights = () => import('components/power/Rights.vue')
const Roles = () => import('components/power/Roles.vue')
const Cate = () => import('components/goods/Cate.vue')
const Params = () => import('components/goods/Params.vue')
const List = () => import('components/goods/List.vue')
const Add = () => import('components/goods/Add.vue')
const Report = () => import('components/report/Report.vue')


Vue.use(VueRouter)

const routes = [
  {
    path: '',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: User
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

//挂载导航守卫
router.beforeEach((to,from,next) => {
  //to代表将要访问哪个路径
  //from代表从哪个路径跳转而来
  //next是函数  next()放行  next('/login') 强制跳转到login页面
  if(to.path === '/login') return next()  //访问login 放行
  const tokenStr = window.sessionStorage.getItem('token');  //获取token
  if(!tokenStr) return next('/login')  //没有token就强制跳转到登录页面
  next();  //有token就放行  注：这里并没有验证token值是否正确
})

export default router
